﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FFShellAPI.FFMPEG.Settings;
using System.Xml.Serialization;
using System.ComponentModel;

namespace AVCWizard
{
    [Serializable]
    public class MediaListItem : INotifyPropertyChanged
    {
        public MediaListItem()
        {
            Audio = new AudioSettings();
            Video = new VideoSettings();
            Range = new RangeSettings();
            Video.MaxFramesPerSeconds = 60;
            SrcLengthTicks = 0;
            SrcFileName = String.Empty;
            SrcBitrate = 0;
            SrcWidth = 0;
            SrcHeight = 0;
            Volume = 0d;
            OutputFormatIndex = -1;
            SrcAudioFormat = String.Empty;
            SrcVideoFormat = String.Empty;
            TargetFileName = String.Empty;
        }

        string _TargetFileName;
        public string TargetFileName
        {
            get { return _TargetFileName; }
            set 
            { 
                _TargetFileName = value;
                OnPropertyChanged("TargetFileName");
            }
        }

        public int OutputFormatIndex { get; set; }
        public long SrcLengthTicks { get; set; }
        public string SrcAudioFormat { get; set; }
        public string SrcVideoFormat { get; set; }
        public int SrcBitrate { get; set; }
        public int SrcWidth { get; set; }
        public int SrcHeight { get; set; }
        public AudioSettings Audio { get; set; }
        public VideoSettings Video { get; set; }
        public RangeSettings Range { get; set; }
        public string SrcFileName { get; set; }

        double _volume;
        public double Volume
        {
            get
            {
                return _volume;
            }
            set
            {
                _volume = value;
                Audio.Volume = 256 + (int)(_volume * 256d);
            }
        }

        [XmlIgnoreAttribute]
        public int AudioBitrate
        {
            get
            {
                return Audio.Bitrate;
            }
            set
            {
                Audio.Bitrate = value;
                OnPropertyChanged("AudioBitrate");
                OnPropertyChanged("AudioQuality");
            }
        }

        [XmlIgnoreAttribute]
        public int VideoWidth
        {
            get
            {
                return Video.PixelWidth;
            }
            set
            {
                Video.PixelWidth = value;
                OnPropertyChanged("VideoWidth");
            }
        }

        [XmlIgnoreAttribute]
        public int VideoHeight
        {
            get
            {
                return Video.PixelHeight;
            }
            set
            {
                Video.PixelHeight = value;
                OnPropertyChanged("VideoHeight");
            }
        }

        [XmlIgnoreAttribute]
        public double AudioQuality
        {
            get
            {
                return AudioBitrate / (double)MainWindow.Config.Current.AppCfg.MaxAudioBitrate;
            }
            set
            {
                AudioBitrate = (int)(value * MainWindow.Config.Current.AppCfg.MaxAudioBitrate);
                OnPropertyChanged("AudioQuality");
            }
        }

        [XmlIgnoreAttribute]
        public int VideoBitrate
        {
            get
            {
                return Video.Bitrate;
            }
            set
            {
                Video.Bitrate = value;
                OnPropertyChanged("VideoBitrate");
                OnPropertyChanged("VideoQuality");
            }
        }

        [XmlIgnoreAttribute]
        public double VideoQuality
        {
            get
            {
                return VideoBitrate / (double)MainWindow.Config.Current.AppCfg.MaxVideoBitrate;
            }
            set
            {
                VideoBitrate = (int)(value * MainWindow.Config.Current.AppCfg.MaxVideoBitrate);
                OnPropertyChanged("VideoQuality");
            }
        }

        [XmlIgnoreAttribute]
        public TimeSpan SrcLength 
        {
            get
            {
                return new TimeSpan(SrcLengthTicks);
            }
            set
            {
                SrcLengthTicks = value.Ticks;
                OnPropertyChanged("SrcLength");
            }
        }

        [XmlIgnoreAttribute]
        public int ScreenFormat
        {
            get
            {
                switch (Video.ScreenFormat)
                {
                    case VideoScreenFormat.FormatNone: return 0;
                    case VideoScreenFormat.FormatNormal: return 1;
                    case VideoScreenFormat.FormatWideScreen: return 2;
                    default: return 0;
                }
            }
            set
            {
                if (value == 0)
                    Video.ScreenFormat = VideoScreenFormat.FormatNone;
                if (value == 1)
                    Video.ScreenFormat = VideoScreenFormat.FormatNormal;
                if (value == 2)
                    Video.ScreenFormat = VideoScreenFormat.FormatWideScreen;
                OnPropertyChanged("ScreenFormat");
            }
        }

        [XmlIgnoreAttribute]
        public bool EnableVideo
        {
            get
            {
                return !Video.DisableVideo;
            }
            set
            {
                Video.DisableVideo = !value;
                OnPropertyChanged("EnableVideo");
            }
        }

        [XmlIgnoreAttribute]
        public bool EnableAudio
        {
            get
            {
                return !Audio.DisableAudio;
            }
            set
            {
                Audio.DisableAudio = !value;
                OnPropertyChanged("EnableAudio");
            }
        }

        public string SrcBitrateString
        {
            get
            {
                return SrcBitrate.ToString() + "kb/s";
            }
        }

        public string SrcLengthString
        {
            get
            {
                return String.Format("{0:00}:{1:00}:{2:00}", SrcLength.Hours, SrcLength.Minutes, SrcLength.Seconds);
            }
        }

        public string SrcRes
        {
            get 
            { 
                return String.Format("{0}x{1}", SrcWidth, SrcHeight); 
            }
        }

        public int AudioChannels
        {
            get
            {
                return Audio.Channels - 1;
            }
            set
            {
                Audio.Channels = value + 1;
                OnPropertyChanged("AudioChannels");
            }
        }

        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
